using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Reflection;
using System.Collections.Specialized;
using System.Diagnostics;
using Meta;

public class RequestHandling
{
	public static void HandleRequest()
	{
		Uri uri = new Uri("http://localhost" + HttpContext.Current.Request.RawUrl);
		string contentType;
		HttpContext context = HttpContext.Current;

		string path = context.Request.PhysicalPath;
		switch (Path.GetExtension(path))
		{
			case ".gif":
				contentType = "image/gif";
				break;
			case ".jpg":
				contentType = "image/jpeg";
				break;
			case ".png":
				contentType = "image/png";
				break;
			default:
				contentType = null;
				break;
		}
		if (contentType != null)
		{
			if (!File.Exists(path))
			{
				context.Response.Status = "Image not found";
				context.Response.StatusCode = 404;
			}
			else
			{
				context.Response.StatusCode = 200;
				context.Response.ContentType = contentType;
				context.Response.WriteFile(path);
			}
			return;
		}

		//for (int i = 1; i < uri.Segments.Length; i++)
		//{
		//    string segment = uri.Segments[i].ToLower().Trim('/');
		//    if (segment != "default.aspx")
		//    {
		//        segment = char.ToUpper(segment[0]) + segment.Substring(1);
		//        //segment = char.ToUpper(segment[0]) + segment.Substring(1);
		//        type = type.GetNestedType(segment);
		//    }
		//}
		//PageBase page=(PageBase)type.GetConstructor(new Type[]{}).Invoke(null);
		//string text=page.Get(context.Request.QueryString);

		DictionaryMap m = new DictionaryMap();
		//var context=HttpContext.Current;
		foreach (string key in context.Request.Params.Keys)
		{
			m[key] = context.Request.Params[key];
		}

		//string text = "hello world";
		string text=callable["get"].Call(m).GetString();
		HttpContext.Current.Response.Write(text);
		HttpContext.Current.RewritePath("~/Default.aspx");
		//HttpContext.Current.Response.End();
	}
	public static void Start()
	{
		callable = Interpreter.Run("C:\\Projects\\Meta\\Web\\code.meta", Map.Empty);
		
	}
	public static Map callable;
}